package com.test.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.test.ssm.pojo.Account;
import com.test.ssm.pojo.Menu;
import com.test.ssm.pojo.Role;
import com.test.ssm.pojo.TableData;
import com.test.ssm.service.AccountService;
import com.test.ssm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("user.html")
public class UserManagerController {
    @Autowired
    private AccountService accountService;

    @Autowired
    private RoleService roleService;

    @RequestMapping
    public String managerPage() {
        return "WEB-INF/pages/account.jsp";
    }

    @RequestMapping(params = "act=userData")
    @ResponseBody
    public TableData table(Account account, Integer page, Integer rows) {
        PageInfo<Account> accountList = accountService.getAccountList(account, page, rows);
        return new TableData(accountList.getTotal(), accountList.getList());
    }

//
//    public String beforeUpdateToGet(){
//        return ""
//    }

    @RequestMapping(params = "beforeUpdateToGet")
    @ResponseBody
    public Account beforeUpdateToGet(Account account) {
        Account accountById = accountService.getAccountById(account);
        return account;
    }

    @RequestMapping(params = "act=delete_user")
    @ResponseBody
    public int deleteUser(Integer[] ids) {
        int i = 0;
        for (Integer id : ids) {
            System.out.println("================" + id);
            i = accountService.deleteUser(id);
        }
        return i;
    }

    @RequestMapping(params = "act=go_add_page")
    public String goAddPage() {
        return "WEB-INF/pages/account_add.jsp";
    }

    @RequestMapping(params = "act=go_add")
    @ResponseBody
    public int goAddAcount(Account account) {
        int i = accountService.addAccount(account);
        return i;
    }

    @RequestMapping(value = "act=go_edit_page")
    @ResponseBody
    public Account goEditPage(Account account) {
        return accountService.getAccountById(account);
    }

    //修改查询
    @RequestMapping(params = "act=go_edit_page")
    public String goEdit(Account account, Model model) {
        System.out.println("================" + account.getId());
        if (account != null) {

            Account acc = accountService.getAccountById(account);
            model.addAttribute("acc", acc);
        }
        return "WEB-INF/pages/account_edit.jsp";
    }

    //更新数据库
    @RequestMapping(params = "act=go_update")
    @ResponseBody
    public int updateAccount(Account account) {
        System.out.println(account.getId() + "=======" + account.getName());
        int i = accountService.updateAccount(account);
        return i;
    }

    //分配权限时获取角色list
    @RequestMapping(params = "go_assign_page")
    public List<Role> goAssignPage(Role role) {
        PageInfo<Role> roleList = roleService.getRoleList(role, null, null);
        return roleList.getList();
    }
}
